package com.more.spring.boot;

import com.more.spring.boot.config.SqlSessionFactoryConfig;
import com.more.spring.boot.mapper.UserByAnnotationMapper;
import com.more.spring.boot.mapper.UserMapper;
import com.more.spring.boot.pojo.Role;
import com.more.spring.boot.pojo.User;
import com.more.spring.boot.pojo.UserDetail;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.*;

public class SelectUser {

    public static void main(String[] args) throws IOException {
        //注册Mapper
//        SqlSessionFactoryConfig.registerMapper(UserMapper.class);
        SqlSessionFactoryConfig.registerMapper(UserByAnnotationMapper.class);
        //构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryConfig.getSqlSessionFactory();


        /*
        updateUser(sqlSessionFactory);
        selectUesrById2(sqlSessionFactory);
        deleteUserById(sqlSessionFactory);
        */
//        insertBatchUser(sqlSessionFactory);

//        selectUserWithAlias(sqlSessionFactory);

//        dynamicSelectUser(sqlSessionFactory);
//        selectRoleUser(sqlSessionFactory);

//        selectUserDetail(sqlSessionFactory);
//        insertAotoIncreUser(sqlSessionFactory);

//        selectAnnotationUserById(sqlSessionFactory);

//        selectAnnotationResultUser(sqlSessionFactory);

        selectAnnotationConstrctUser(sqlSessionFactory);
    }


    /**
     * 根据ID查询用户信息
     * @param sqlSessionFactory
     */
    public static void selectUesrById( SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = mapper.selectUesrById(27);
            System.out.println(user);
        }
    }

    /**
     * 根据ID查询用户信息
     * @param sqlSessionFactory
     */
    public static void selectUesrById2( SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            HashMap<String, String> map = mapper.selectUesrById2(27);
            Set<Map.Entry<String, String>> entries = map.entrySet();

            for(Map.Entry<String, String> set:entries){
                System.out.print(set.getKey() + "=" + set.getValue() +"\t");
            }

            System.out.println();
        }
    }

    public static void updateUser(SqlSessionFactory sqlSessionFactory){
        User user = new User(27, "喵喵喵", "男");
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.updateUser(user);
            System.out.println("更新结果:" + i);
            sqlSession.commit();
        }
    }

    public static void deleteUserById(SqlSessionFactory sqlSessionFactory){
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.deleteUserById(555);
            System.out.println("删除结果:" + i);
            sqlSession.commit();
        }
    }

    public static void insertBatchUser(SqlSessionFactory sqlSessionFactory){
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

            ArrayList<User> userList = new ArrayList<>();
            userList.add(new User(0, "测试1", "男"));
            userList.add(new User(0, "测试2", "女"));
            userList.add(new User(0, "测试3", "女"));
            userList.add(new User(0, "测试4", "男"));
            int i = mapper.insertBatchUser(userList);
            System.out.println("批量插入结果:" + i);
            sqlSession.commit();
        }
    }

    /**
     * 动态查询 将<sqo>的参数传入<include>
     * @param sqlSessionFactory
     */
    public static void selectUserWithAlias( SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = mapper.selectUserWithAlias(27);

            System.out.println(user);

            System.out.println();
        }
    }



    public static void dynamicSelectUser(SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

            List<User> users = mapper.dynamicSelectUser(false);
            for (User user : users) {
                System.out.println(user);
            }
        }
    }


    public static void selectRoleUser(SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

            List<Role> roles = mapper.selectRoleUser();
            for (Role role : roles) {
                System.out.println(role);
            }
        }
    }


    public static void selectUserDetail(SqlSessionFactory sqlSessionFactory){
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

            List<UserDetail> userDetails = mapper.selectUserDetail();
            for (UserDetail userDetail : userDetails) {
                System.out.println(userDetail);
            }
        }
    }


    public static void insertAotoIncreUser(SqlSessionFactory sqlSessionFactory){
        User user = new User(27, "喵喵喵4", "男");
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserByAnnotationMapper mapper = sqlSession.getMapper(UserByAnnotationMapper.class);
//            int i = mapper.insertAotoIncreUser(user);
//            System.out.println("更新结果:" + i);
            sqlSession.commit();
        }
    }

    public static void insertAotoIncreUser2(SqlSessionFactory sqlSessionFactory){
        User user = new User(27, "喵喵喵6", "男");
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserByAnnotationMapper mapper = sqlSession.getMapper(UserByAnnotationMapper.class);
            int i = mapper.insertAotoIncreUser2(user.getUname(), user.getGender());
            System.out.println("更新结果:" + i);
            sqlSession.commit();
        }
    }

    public static void selectAnnotationUserById(SqlSessionFactory sqlSessionFactory){
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserByAnnotationMapper mapper = sqlSession.getMapper(UserByAnnotationMapper.class);
            User user = mapper.selectAnnotationUserById(27);
            /*for (User user:users){
                System.out.println(user);
            }*/
            System.out.println(user);
//            sqlSession.commit();
        }
    }


    public static void selectAnnotationResultUser(SqlSessionFactory sqlSessionFactory){
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserByAnnotationMapper mapper = sqlSession.getMapper(UserByAnnotationMapper.class);
            List<User> users = mapper.selectAnnotationResultUser();
            for (User user:users){
                System.out.println(user);
            }

        }
    }



    public static void selectAnnotationConstrctUser(SqlSessionFactory sqlSessionFactory){
        try(SqlSession sqlSession = sqlSessionFactory.openSession()){
            UserByAnnotationMapper mapper = sqlSession.getMapper(UserByAnnotationMapper.class);
            List<User> users = mapper.selectAnnotationConstrctUser();
            for (User user:users){
                System.out.println(user);
            }

        }
    }
}
